<?php

class Core_Action_Admin_System_Appearance_Section_Edit extends Action_Admin
{
	public function execute($request, $initial = false, $script = null)
	{
		if ($initial)
		{
			Agilet::setVar('breadcrum', array(
				array(
					'url' => 'admin/system',
					'label' => 'System'
				),
				array(
					'url' => 'admin/system/appearance',
					'label' => 'Appearance'
				),
				array(
					'url' => 'admin/system/appearance/section',
					'label' => 'Section'
				),
				array(
					'url' => 'admin/system/appearance/section/edit',
					'label' => 'Edit'
				),
			));
			
		}

		$table = Model::getTable('core/appearance/section');

		$select = $table -> select();

		$form = Form::factory('core.form.admin.appearance.section');

		$id = $request -> getParam('id');

		$item = $table -> find((int)$id) -> current();

		if (!$item)
		{
			return;
		}

		if ($request -> isGet())
		{
			$form -> setData($item -> toArray());
		}

		if ($request -> isPost() and $form -> isValid($_POST))
		{
			try
			{
				$data = $form -> getData();

				$item -> fromArray($data);
				$item -> save();

				$form -> setNotice('Saved.');

				Response::redirect('admin/system/appearance/section');

			}
			catch(Exception $e)
			{
				DEBUG and $form -> setNotice($e -> getMessage());
			}
		}

		$request -> response = View::render($script = 'core/html/admin/edit', $data = array(
			'form' => $form,
			'title' => 'Add Section',
		));
	}

}
